var User = require('../../models/user/user')
var Suggest = require('../../models/user/userSuggest')
var Apply = require('../../models/user/applyManage')

var config = require('../../config/config')



module.exports = {
    //  登陆，返回前端管理权限
    login: function (req, res) {
        var query = req.query

        var name = query.name
        var password = query.password

        if (config.admin.name == name && config.admin.password == password) {
            return res.json({code: 100, data: {identity: 2}})
        }
        User.findOne({name: name}, function (err, user) {
            if (err) {
                console.log('adminController login fail. err = ' + err)
                return res.json({code: 177, msg: '登陆出错'})
            }
            if (!user) {
                return res.json({code: 122, msg: '用户不存在'})
            }
            if (!user.identity) {
                return res.json({code: 122, msg: '无权登陆'})
            }
            if (password != user.password) {
                return res.json({code: 122, msg: '密码错误'})
            }
            return res.json({code: 100, data: {identity: user.identity}})
        })
    },
    getAdmins: function (req, res) {

        User.find({identity: {$gte: 1}}, function (err, users) {
            if (err) {
                console.log('adminController getAdmins fail. err = ' + err)
                return res.json({code: 177, msg: '获取失败'})
            }
            return res.json({code: 100, data: {list: users}})
        })

    }
}